from config import *
from help import *
from gui_sub import *

class my3(QWidget):
    def __init__(self):
        super().__init__()                                            
        self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.ToolTip)
        self.setGeometry (100, 100, main_win_size[0], main_win_size[1])
        self.l = QLabel(self)
        self.setIni()
        h = QHBoxLayout(self)
        self.l.setStyleSheet("QLabel {border: %spx solid rgb(0, 0, 0); background-color : white;  }" % main_win_border);        
        h.addWidget(self.l)
        d = 0
        h.setContentsMargins(d, d, d, d)
        self.setLayout(h)
        self.co = 0
        t = QTimer(self)
        t.timeout.connect(self.ont)
        t.start(20)
        self.last = None
        self.offset = None
        self.p = True
        self.t = ''        
        
        if os.path.exists(icon_file_path):
            self.tray = QSystemTrayIcon(self)
            self.tray.activated.connect(self.trayA)
            self.tray.setToolTip('mm')
            self.tray.setIcon(QIcon(icon_file_path))
            self.tray.show()
            
        
    def trayA(self,a): 
        if a == self.tray.Trigger:            
            cfg.pause_key = not cfg.pause_key
            
        if a == self.tray.Context:
            sys.exit(0)
            
    def closeEvent(self, a0):
        print('close')
        import sys
        sys.exit(0)
        
    def setIni(self):
        self.resize(main_win_size[0], main_win_size[1]) 
        self.show()
        
    def mouseMoveEvent(self, event):
        if self.offset is not None:
            self.move(self.pos() - self.offset + event.pos())
            
    def mousePressEvent(self, event):
        a =  event.button()
        print(a)
        if a == Qt.RightButton:
            self.hide()
        if a == Qt.MiddleButton:
            import sys
            sys.exit(0)
        if a == Qt.LeftButton:                    
            self.offset = event.pos()
   
    def mouseReleaseEvent(self, event):
        a =  event.button()
        if a == Qt.LeftButton:
            self.offset = None
            
    def get_text(self):
        if cfg.cmd_mode:
            return 'C'
        if cfg.set_mode:
            return "S"
        
        x, y = getp()
        sx, sy = cfg.fix_pos
        dx = x - sx
        dy = y - sy
        
        if cfg.isShow:
            ss = '□'
        elif cfg.show_one_on:
            ss = '♡'
            '️♥'
            if not cfg.w2showing:
                ss = '△'
        else:
            ss = '▽'
            if not cfg.always_show_one:
                ss = '♥'
                '△'
        if cfg.force_show:
            ss += 'F'
        lo = '↹'
        if not cfg.lock:
            lo = ''
        if cfg.lock and cfg.lock_xing_mode:
            st = ' *%s%s %s↻ (%s,%s)%s %s' % ('{:2s}'.format(cfg.sum), get_arr(dx,dy),sca_l[cfg.sca % len(sca_l)],
                                                cfg.lock_xing_last_move[0], cfg.lock_xing_last_move[1],lo,ss)
        elif cfg.lock and cfg.lock_jia_mode:
            st = ' +%s%s %s↻ (%s,%s)%s %s' % ('{:2s}'.format(cfg.sum), get_arr(dx,dy),sca_l[cfg.sca % len(sca_l)],
                                                cfg.lock_xing_last_move[0], cfg.lock_xing_last_move[1],lo,ss)
        else:
            st = ' %s%s %s↻ (%s)%s %s' % ('{:2s}'.format(cfg.sum), get_arr(dx,dy),sca_l[cfg.sca % len(sca_l)], lock_get(), lo,ss)
            
        if cfg.is_pressed:
            st += '╩'
       
        if cfg.x2_mode:
            st += '。'
        return st
    
    def ont(self):                
        cur = get_cur_pos()
        sign =  (cfg.isShow, cfg.fix_pos, cur, cfg.sum,cfg.sca, cfg.sca_du, cfg.sca_cur, 
                 cfg.sca_cur_pos, cfg.lock,
                  cfg.lock_step, cfg.pause_key,
                 cfg.pause_move, cfg.is_pressed, cfg.move_to_win, cfg.cmd_mode, cfg.set_mode,
                   cfg.show_one_on, cfg.always_show_one, cfg.force_show, cfg.w2showing,
                  cfg.lock_xing_mode,cfg.lock_jia_mode, cfg.lock_xing_last_move, cfg.x2_mode)
        if self.last == sign:
            return
        
        if cfg.move_to_win:
            cfg.move_to_win = False
            geo = self.geometry()
            mouse.Controller().position = (geo.x(), geo.y())
            
        if self.p != cfg.pause_key:
            print('pp')
            self.p = cfg.pause_key
            if self.isHidden():
                self.setIni()
            else:
                self.hide()
                        
        if cfg.pause_move:
            self.move(cfg.pause_move[0], cfg.pause_move[1])
            self.setIni()
            cfg.pause_move = False
            
        self.last = sign
        st = self.get_text()
        
        if self.t == st:
            return
        
        #if change_win_color:
            #if '■' in st:
                #self.l.setStyleSheet("QLabel {border: %spx solid rgb(0, 0, 0); background-color : rgb(0, 255, 0);  }" % main_win_border);
            #else:
                #self.l.setStyleSheet("QLabel {border: %spx solid rgb(0, 0, 0); background-color : white;  }" % main_win_border);
        self.t = st
        self.l.setText(st)
        f = QFont("Times", main_win_txt_size);
        f.setBold(True)
        self.l.setFont(f);
        
class my4(QMainWindow):
    def __init__(self):
        super().__init__()  
        self.setWindowFlags(Qt.WindowStaysOnTopHint|Qt.FramelessWindowHint|Qt.ToolTip)                
        self.setStyleSheet("background-color:black;")                    
        self.hide()
        self.setGeometry(200, 200,32, 32)                         
        icon_g = QIcon(dst_arr)     
        self.mouse_cursor_pixmap = icon_g.pixmap(icon_g.actualSize(QSize(32, 32)))
        t = QTimer(self)
        t.timeout.connect(self.ont)
        t.start(20)
        self.si = []
        
    def ont(self):
        si = (cfg.x2_mode, cfg.x2_mode_dst[0], cfg.x2_mode_dst[1])
        if self.si == si:
            return
        self.si = si
        if cfg.x2_mode:            
            self.move(cfg.x2_mode_dst[0], cfg.x2_mode_dst[1])
            self.show()
        else:
            self.hide()
        
    def paintEvent(self,e):                 
        p = QPainter(self)         
        ma = self.mouse_cursor_pixmap.mask()                   
        self.setMask(ma)                             
        p.setRenderHint(QPainter.Antialiasing);        
        p.drawPixmap(0,0,32,32,self.mouse_cursor_pixmap)
if __name__ == '__main__':
    app = QApplication([])   
    w = my4()
        
    app.exec_()     